//
//  KwSegmentView.m
//  KuWoCan
//
//  Created by hwd on 17/3/13.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "KwSegmentView.h"

#define lineWidth 10

@interface KwSegmentView ()

@property (nonatomic,strong) UIView *lineView;

@property (nonatomic,assign) NSInteger selectNum;

@end

@implementation KwSegmentView

- (instancetype)initWithBtnNames:(NSArray *)btnNames
{
    if (self = [super init])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:KwSegViewNotification object:nil];
        
        //所有btn的宽度和
        float allWidth = 0.0;
        for (int x = 0; x<btnNames.count; x++) {
            float aWidth = [KwSize boundingRectWithText:btnNames[x] Font:[UIFont systemFontOfSize:14.0]].width;
            allWidth = aWidth+allWidth;
        }
        
        //btn之间的间距
        float y = ([UIScreen mainScreen].bounds.size.width - allWidth-40)/(btnNames.count-1);
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = KwBlueColor;
        [self addSubview:_lineView];

        _btnNames = btnNames;
        
        for (int i = 0; i<btnNames.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [UIColor whiteColor];
            btn.tag = 100 + i;
            
            [btn setTitle:btnNames[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [btn setTitleColor:KwBlueColor forState:UIControlStateSelected];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
            btn.titleLabel.textColor = [UIColor blackColor];
            [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
        }
        
        for (int i = 0; i<btnNames.count; i++) {
            UIButton *currtenBtn = (UIButton *)[self viewWithTag:100+i];
            UIButton *nextBtn = (UIButton *)[self viewWithTag:100+i+1];
            UIButton *lastBtn = (UIButton *)[self viewWithTag:100+i-1];
            [currtenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(@10);
                make.height.equalTo(@30);
                make.bottom.equalTo(_lineView.mas_top);
                if (i == 0)
                {
                    make.left.equalTo(@15);
                    make.right.equalTo(nextBtn.mas_left).offset(-y);
                }
                else if(i == btnNames.count-1)
                {
                    make.right.equalTo(@-15);
                    make.left.equalTo(lastBtn.mas_right).offset(y);
                }
                else
                {
                    make.left.equalTo(lastBtn.mas_right).offset(y);
                    make.right.equalTo(nextBtn.mas_left).offset(-y);
                }
            }];
        }
        
        //设置默认状态
//        __weak typeof(self) weakSelf = self;
        UIButton *firstBtn = (UIButton *)[self viewWithTag:100];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make)
         {
             make.height.equalTo(@1.5);
             make.width.equalTo(@(lineWidth));
//             make.left.equalTo(@([weakSelf getOriginX:firstBtn]));
             make.left.equalTo(@30);
         }];
        
        firstBtn.selected = YES;
    }
    return self;
}

//- (void)setBtnNames:(NSArray *)btnNames
//{
//    __weak typeof(self) weakSelf = self;
//    UIButton *firstBtn = (UIButton *)[self viewWithTag:100];
//    [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@([weakSelf getOriginX:firstBtn]));
//    }];
//}

//收到通知后操作
- (void)getNotification:(NSNotification *)notification
{
    NSInteger seletIndex = [notification.userInfo[KwNotificationInfoKey_Index] integerValue];
    UIButton *aBtn = (UIButton *)[self viewWithTag:100 + seletIndex];
    [self changeStatusWithBtn:aBtn];
}

//点击后操作
- (void)clickAction:(UIButton *)btn
{
    if (_cancleBlock) {
        self.cancleBlock();
    }
    KwSegViewHelper *kwHelper = [[KwSegViewHelper alloc] init];
    kwHelper.kwSegIndex = btn.tag-100;
}

- (void)changeStatusWithBtn:(UIButton *)btn
{
    for (int i = 0; i<_btnNames.count; i++)
    {
        UIButton *aBtn = (UIButton *)[self viewWithTag:100+i];
        
        if (aBtn.tag == btn.tag)
        {
            aBtn.selected = YES;
        }
        else
        {
            aBtn.selected = NO;
        }
    }
    __weak typeof(self) weakSelf = self;
    [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([weakSelf getOriginX:btn]));
    }];
}

- (CGFloat)getOriginX:(UIButton *)btn
{
    float lineX = (btn.bounds.size.width - lineWidth)/2+btn.origin.x;
    return lineX;
}

- (void)dealloc
{
    [[NSNotificationCenter
      defaultCenter] removeObserver:self];
}

@end
